package ACEbrickBreaker;

/**
 * <p>Title: ImagePanel</p>
 * <p>Description: ImagePanel
 *       This panel sets up the images for the game based on Brick, Ball, and Paddle objects. It attempts to set up a background image for the game but it is not working. (We didn't have time to focus on it, but will go back to it in the future)</p>
 * @author Caitlin Finn
 * @email caitlin.finn@my.uwrf.edu
 * @author Erin McNeil
 * @email erin.mcneil@my.uwrf.edu
 * @author Andrew Juranek
 * @email andrew.juranek@my.uwrf.edu
 * @date December 10th 2012
 */
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

public class ImagePanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// declares array of bricks, ball, & paddle to be drawn
	private final Brick[] bricks;
	private final Ball ball;
	private final Paddle paddle;
	private Image backGround;// = getImage(getCodeBase(),
								// "handpainted.jpg");

	/**
	 * constructor of panel takes in copies of the bricks array, ball, & paddle
	 * 
	 * @param bricks
	 * @param ball
	 * @param paddle
	 * @param background
	 */
	public ImagePanel(Brick[] bricks, Ball ball, Paddle paddle, Image background) {
		super();
		this.bricks = bricks;
		this.ball = ball;
		this.paddle = paddle;
		this.backGround = background;
		// sets border of panel and background color
		setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 4));

		setBackground(Color.black);

		// setBackGroundImage(backGround);
		// sets focusable of panel to allow key press
		this.setFocusable(true);

		// paints initial position upon instantiation
		repaint();
	}

	/**
	 * This method should set the background image if we had it working
	 * correctly
	 * 
	 * @param backGround
	 */
	public void setBackGroundImage(Image backGround) {
		this.backGround = backGround;
	}

	/**
	 * returns background image
	 * 
	 * @return backGround
	 */
	private Image getBackGroundImage() {
		return backGround;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
	 */
	@Override
	public void paintComponent(Graphics g) {

		super.paintComponent(g);
		setBackGroundImage(backGround);
		// get the size of this panel(which is the size of the applet) and
		// draw the image
		g.drawImage(getBackGroundImage(), 0, 0, (int) getBounds().getWidth(),
				(int) getBounds().getHeight(), this);

		// paints bricks only if they are not hit
		for (int i = 0; i < bricks.length; i++) {
			if (!bricks[i].isHit()) {
				switch (bricks[i].getColorCode()) {
				case 1:
					g.setColor(Color.PINK);
					break;
				case 2:
					g.setColor(Color.cyan);
					break;
				case 3:
					g.setColor(Color.GREEN);
					break;
				case 4:
					g.setColor(Color.MAGENTA);
					break;
				}
				g.fillRect(bricks[i].getX(), bricks[i].getY(), 100, 45 / 2);
				g.setColor(Color.BLACK);
				g.drawRect(bricks[i].getX(), bricks[i].getY(), 100, 45 / 2);
			}
		}

		// paints paddle
		g.setColor(Color.GRAY);
		g.fillRect(paddle.getX(), paddle.getY(), 100, 10);
		g.setColor(Color.DARK_GRAY);
		g.drawRect(paddle.getX(), paddle.getY(), 100, 10);

		// paints ball
		g.setColor(Color.WHITE);
		g.fillOval(ball.getX(), ball.getY(), 15, 15);

	}

	// method that calls the repaint method
	/**
	 * updateBoard() repaints the board after we check for the contact of the
	 * ball hitting bricks or game over
	 */
	public void updateBoard() {
		repaint();

	}

}
